<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 2018/10/11
 * Time: 14:03
 */

namespace home\members\wodrow\profile_box;

use common\models\db\User;
use kartik\widgets\Widget;
use kittools\webuipopover\WebUIPopoverAsset;

class ProfileBox extends Widget
{
    public $body;
    public $options;
    /**
     * @var User $user
     */
    public $user;

    public function run()
    {
        WebUIPopoverAsset::register($this->view);
        $uni_class = "popover-user-profile-box-{$this->user->id}";
        $this->regJs($uni_class);
        $this->options['class'] = isset($this->options['class'])?$this->options['class']." popover-user-profile-box":"popover-user-profile-box";
        $this->options['class'] .= " ".$uni_class;
//        $this->options['data-content'] = $this->render('box', ['user' => $this->user]);
        return $this->render('index', [
            'body' => $this->body,
            'options' => $this->options,
            'user' => $this->user,
        ]);
    }

    public function regJs($c)
    {
        $js = <<<JS
$(function() {
    $("a.{$c}").webuiPopover({
        trigger: 'hover',
        type:'async',
        url:"{$this->user->profileBoxUrl}",
        content:function(data){
            var html = data;
            return html;
        }
    });
})
JS;
        $this->registerWidgetJs($js);
    }

}